#########################################################################
#########################################################################
####Non-state Atrocities in Capital Cities - Mechanism 3 (FDI Plots) ####
#########################################################################
#########################################################################
library(foreign)
library(plyr)
library(DataCombine)
library(doBy)
library(countrycode)
library(ggplot2)

# Set working library
setwd("~/Data/Mechanism 3/")
# Read in FDI data
FDI.dat <- read.csv("FDIdatmain.csv")
# Read in conflict data
at.dat.count <- read.dta("insatdatacount.dta")


###########
###India###
###########
# Subset India atrocities data
at.dat.in <- subset(at.dat.count, ccode==750)
# Subset India FDI data
FDI.dat.in <- FDI.dat[,c("year", "India_FDI")]
# Merge the two
ind.dat <- join(at.dat.in, FDI.dat.in)
# Create a logged version of the FDI variable
ind.dat$log_FDI <- log(ind.dat$India_FDI+1)

###Plot
#pdf("indfdi.pdf", width=10)
## Plot first set of data and draw its axis
par(mai=c(1,2,1,2))
plot(ind.dat$year, ind.dat$NSA_at_sum, pch=16, axes=FALSE, ylim=c(0,50), xlab="", ylab="", 
     type="b",col="red", main="Insurgent Atrocities and FDI Flows (India)")
axis(2,col="black",las=1)  ## las=1 makes horizontal labels
mtext("NSA Atrocities",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(ind.dat$year, ind.dat$log_FDI, pch=15,  xlab="", ylab="", ylim=c(21,25), 
     axes=FALSE, type="b", col="green")
## a little farther out (line=4) to make room for labels
axis(4,  col="black",col.axis="black",las=2)
mtext(side=4, line=4, 'FDI (Log)') 

## Allow a second plot on the same graph
par(new=TRUE)

## Draw the time axis
axis(1,pretty(range(ind.dat$year),10))
mtext("Years",side=1,col="black",line=2.5)  
## Add Legend
legend("topleft",legend=c("Insurgent Atrocities","FDI (Log)"),
       text.col=c("red", "green"),pch=c(16,15),col=c("red","green"))
#dev.off()

############
###Israel###
############
# Subset Israel atrocities data
at.dat.is <- subset(at.dat.count, ccode==666)
# Subset Israel FDI data
FDI.dat.is <- FDI.dat[,c("year", "Israel_FDI")]
# Merge the two
is.dat <- join(at.dat.is, FDI.dat.is)
# Create a logged version of the FDI variable
is.dat$log_FDI <- log(is.dat$Israel_FDI+1)

###Plot
#pdf("isfdi.pdf", width=10)
## Plot first set of data and draw its axis
par(mai=c(1,2,1,2))
plot(is.dat$year, is.dat$NSA_at_sum, pch=16, axes=FALSE, ylim=c(0,25), xlab="", ylab="", 
     type="b",col="red", main="Insurgent Atrocities and FDI Flows (Israel)")
axis(2,col="black",las=1)  ## las=1 makes horizontal labels
mtext("NSA Atrocities",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(is.dat$year, is.dat$log_FDI, pch=15,  xlab="", ylab="", ylim=c(20,25), 
     axes=FALSE, type="b", col="green")
## a little farther out (line=4) to make room for labels
axis(4,  col="black",col.axis="black",las=2)
mtext(side=4, line=4, 'FDI (Log)') 

## Allow a second plot on the same graph
par(new=TRUE)

## Draw the time axis
axis(1,pretty(range(is.dat$year),10))
mtext("Years",side=1,col="black",line=2.5)  
## Add Legend
legend("topleft",legend=c("Insurgent Atrocities","FDI (Log)"),
       text.col=c("red", "green"),pch=c(16,15),col=c("red","green"))
#dev.off()


#############
###Algeria###
#############
# Subset Algeria atrocities data
at.dat.ag <- subset(at.dat.count, ccode==615)
# Subset Algeria FDI data
FDI.dat.ag <- FDI.dat[,c("year", "Algeria_FDI")]
# Merge the two
ag.dat <- join(at.dat.ag, FDI.dat.ag)
# Create a logged version of the FDI variable
ag.dat$log_FDI <- log(ag.dat$Algeria_FDI+1)

###Plot
#pdf("agfdi.pdf", width=10)
## Plot first set of data and draw its axis
par(mai=c(1,2,1,2))
plot(ag.dat$year, ag.dat$NSA_at_sum, pch=16, axes=FALSE, ylim=c(0,135), xlab="", ylab="", 
     type="b",col="red", main="Insurgent Atrocities and FDI Flows (Algeria)")
axis(2,col="black",las=1)  ## las=1 makes horizontal labels
mtext("NSA Atrocities",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(ag.dat$year, ag.dat$log_FDI, pch=15,  xlab="", ylab="", ylim=c(19,22), 
     axes=FALSE, type="b", col="green")
## a little farther out (line=4) to make room for labels
axis(4,  col="black",col.axis="black",las=2)
mtext(side=4, line=4, 'FDI (Log)') 

## Allow a second plot on the same graph
par(new=TRUE)

## Draw the time axis
axis(1,pretty(range(ag.dat$year),10))
mtext("Years",side=1,col="black",line=2.5)  
## Add Legend
legend("topleft",legend=c("Insurgent Atrocities","FDI (Log)"),
       text.col=c("red", "green"),pch=c(16,15),col=c("red","green"))
#dev.off()


##############
###Colombia###
##############
# Subset Colombia atrocities data
at.dat.col <- subset(at.dat.count, ccode==100)
# Subset Colombia FDI data
FDI.dat.col <- FDI.dat[,c("year", "Colombia_FDI")]
# Merge the two
col.dat <- join(at.dat.col, FDI.dat.col)
# Create a logged version of the FDI variable
col.dat$log_FDI <- log(col.dat$Colombia_FDI+1)

###Plot
#pdf("colfdi.pdf", width=10)
## Plot first set of data and draw its axis
par(mai=c(1,2,1,2))
plot(col.dat$year, col.dat$NSA_at_sum, pch=16, axes=FALSE, ylim=c(0,41), xlab="", ylab="", 
     type="b",col="red", main="Insurgent Atrocities and FDI Flows (Colombia)")
axis(2,col="black",las=1)  ## las=1 makes horizontal labels
mtext("NSA Atrocities",side=2,line=2.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right
plot(col.dat$year, col.dat$log_FDI, pch=15,  xlab="", ylab="", ylim=c(21,24), 
     axes=FALSE, type="b", col="green")
## a little farther out (line=4) to make room for labels
axis(4,  col="black",col.axis="black",las=2)
mtext(side=4, line=4, 'FDI (Log)') 

## Allow a second plot on the same graph
par(new=TRUE)

## Draw the time axis
axis(1,pretty(range(col.dat$year),10))
mtext("Years",side=1,col="black",line=2.5)  
## Add Legend
legend("topleft",legend=c("Insurgent Atrocities","FDI (Log)"),
       text.col=c("red", "green"),pch=c(16,15),col=c("red","green"))
#dev.off()